/*
 * Funny Language - a free style programming language.
 * Copyright (C) 2015 by fanguangping (fanguangping@163.com)
 * cmemory.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "cmemory.h"
#include "../support/gc/gc.h"

void sys_memory_init() {
    GC_INIT();
}

void sys_memory_finish() {
    //no need
}

void *sys_malloc_atomic(size_t __size) {
    void* p = GC_MALLOC_ATOMIC(__size);
    if (p == NULL) {
        fprintf(stderr, "out of memory!\n");
        exit(1);
    }
    return p;
}

void *sys_malloc (size_t __size) {
    void* p = GC_MALLOC(__size);
    if (p == NULL) {
        fprintf(stderr, "out of memory!\n");
        exit(1);
    }
    return p;
}

void *sys_calloc (size_t __nmemb, size_t __size) {
    size_t size = __nmemb * __size;
    void* p = GC_MALLOC(size);
    if (p == NULL) {
        fprintf(stderr, "out of memory!\n");
        exit(1);
    }
    memset(p, 0, size);
    return p;
}

void *sys_realloc (void *__ptr, size_t __size) {
    void* p = GC_MALLOC(__size);
    if (p == NULL) {
        fprintf(stderr, "out of memory!\n");
        exit(1);
    }
    if (__ptr == NULL) {
        return p;
    }
    memcpy(p, __ptr, __size);
    return p;
}

void sys_free (void *__ptr) {
    //no need
}
